package com.zuxia.dao.impl;

import java.util.List;



import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.zuxia.dao.IAppintmentDao;
import com.zuxia.entity.Appointment;
@Component
public class AppintmentDao implements IAppintmentDao{
	
	@Autowired
	private SessionFactory sf;
	@Override
	public List selectAppointment(int pageNow, String yname , int pageSize) {
		
		Session session = sf.getCurrentSession();
		Criteria criteria = null;
		if(yname != null || yname != ""){
			criteria = session.createCriteria(Appointment.class);
			criteria.add(Restrictions.like("appointmentName", "%"+yname+"%"));
		}else{
			criteria = session.createCriteria(Appointment.class);
		}
		criteria.setFirstResult((pageNow - 1) * pageSize);
		criteria.setMaxResults(pageSize);
		return  criteria.list();
	}
	@Override
	public int appsize() {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.createCriteria(Appointment.class).list().size();
		 
	}
	@Override
	public void delapp(Appointment appointment) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.delete(appointment);
		tran.commit();
		session.close();
	}
	@Override
	public void updapp(Appointment appointment) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.update(appointment);	
		tran.commit();
		session.close();
	}
	@Override
	public void addapp(Appointment appointment) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.save(appointment);
		tran.commit();
		session.close();
	}
}
